# -*- coding: utf-8 -*-
# @Time : 2022/3/28 11:19

from Common.mysql import db
from Common.public_method import utc_8
from src.ApiTest.ApiInfo.Database.api_database import ApiInfo
from Common.yaml_method import YamlMethod


class AddApi:
    """
    新增基础接口
    """
    @staticmethod
    def add_api(api_name, project_id, project_name, system_name, module_name, path, method, pre_parameter, headers, body,
                after_parameter, assert_data, create_user):
        """
        新增基础接口
        :param api_name: 接口名称
        :param project_id: 项目ID
        :param project_name: 项目名称
        :param system_name: 系统名称
        :param module_name: 模块名称
        :param path: 请求路径
        :param method: 请求方法
        :param pre_parameter: 前置参数
        :param headers: 请求头
        :param body: 请求体
        :param after_parameter: 响应参数提取
        :param assert_data: 断言参数提取
        :param create_user: 创建人
        :return:
        """

        code = YamlMethod().read_data('code.yaml')['code']

        info = ApiInfo.query.filter_by(apiName=api_name, projectId=project_id).first()
        if info is None:
            create_time = utc_8()
            # 插入数据库
            info = ApiInfo(apiName=api_name, projectId=project_id, projectName=project_name, systemName=system_name,
                           moduleName=module_name, path=path, method=method, pre_parameter=pre_parameter, headers=headers,
                           body=body, after_parameter=after_parameter, assert_data=assert_data, create_time=create_time, create_user=create_user)
            db.session.add(info)
            db.session.commit()

            res = {
                'code': code[0],
                'message': 'success',
                'data': {}
            }
        else:
            res = {
                'code': code[1],
                'message': '配置名称已存在',
                'data': {}
            }
        return res
